package com.project.medical.sys.service.impl;

import com.project.medical.sys.common.utils.PageUtils;
import com.project.medical.sys.common.utils.Query;
import com.project.medical.sys.entity.PatientEntity;
import com.project.medical.sys.service.PatientService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.project.medical.sys.dao.DoctorDao;
import com.project.medical.sys.entity.DoctorEntity;
import com.project.medical.sys.service.DoctorService;


@Service("doctorService")
public class DoctorServiceImpl extends ServiceImpl<DoctorDao, DoctorEntity> implements DoctorService {
    @Autowired
    PatientService patientService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<DoctorEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(key)) {

            wrapper.eq("id", key).or().like("name", key);
        }
        IPage<DoctorEntity> page = this.page(
                new Query<DoctorEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public boolean removeDoctorByIds(List<Integer> asList) {
        for (int i :
                asList) {
            PatientEntity doctor = patientService.getOne(new QueryWrapper<PatientEntity>().eq("doctor_id", i));
            if (doctor!=null){
                return false;
            }
        }
        int i = baseMapper.deleteBatchIds(asList);
        return i>0;
    }

}